package com.teacher.game.framework.util;

import android.graphics.Color;
import com.teacher.game.framework.util.Painter;

/**
 * 粒子类 - 用于创建各种特效
 * 支持位置、速度、生命周期、颜色等属性
 * 增强版：支持多种粒子类型、加速度、旋转、尺寸变化等
 */
public class Particle {

    // 粒子类型常量
    public static final int TYPE_CIRCLE = 0;    // 圆形粒子
    public static final int TYPE_RECT = 1;      // 矩形粒子
    public static final int TYPE_POINT = 2;     // 点状粒子
    public static final int TYPE_TEXT = 3;      // 文字粒子
    
    // 位置坐标
    private float x, y;
    
    // 速度和加速度
    private float velocityX, velocityY;
    private float accelerationX, accelerationY;
    
    // 生命周期
    private float maxLife;
    private float currentLife;
    
    // 视觉属性
    private int color;
    private float size;
    private float alpha;
    private float sizeVelocity;      // 尺寸变化速度
    private float alphaDecay;        // 透明度衰减率
    
    // 旋转属性
    private float rotation;          // 旋转角度
    private float rotationSpeed;     // 旋转速度
    
    // 物理属性
    private float gravity;
    private float friction;
    private float bounce;            // 弹性系数
    
    // 粒子类型和形状
    private int type;
    private String text;             // 文字内容（用于文字粒子）
    
    // 状态
    private boolean active;
    
    /**
     * 默认构造函数
     */
    public Particle() {
        reset();
    }

    /**
     * 构造函数 - 创建指定位置的粒子
     */
    public Particle(float x, float y) {
        this();
        this.x = x;
        this.y = y;
    }
    
    /**
     * 初始化粒子 - 基础版本
     */
    public void init(float x, float y, float velX, float velY, float life, int color, float size) {
        this.x = x;
        this.y = y;
        this.velocityX = velX;
        this.velocityY = velY;
        this.maxLife = life;
        this.currentLife = life;
        this.color = color;
        this.size = size;
        this.alpha = 1.0f;
        this.gravity = 0.0f;
        this.friction = 1.0f;
        this.active = true;
        this.type = TYPE_CIRCLE;
        this.text = "";
        this.accelerationX = 0;
        this.accelerationY = 0;
        this.rotation = 0;
        this.rotationSpeed = 0;
        this.sizeVelocity = 0;
        this.alphaDecay = 0.02f;
        this.bounce = 0;
    }

    /**
     * 初始化粒子 - 增强版本
     */
    public void initAdvanced(float x, float y, float velX, float velY, float life, 
                           int color, float size, int type, float gravity, float friction) {
        init(x, y, velX, velY, life, color, size);
        this.type = type;
        this.gravity = gravity;
        this.friction = friction;
    }

    /**
     * 初始化文字粒子
     */
    public void initTextParticle(float x, float y, float velX, float velY, float life,
                               int color, String text) {
        init(x, y, velX, velY, life, color, 12.0f); // 默认字体大小
        this.type = TYPE_TEXT;
        this.text = text;
    }
    
    /**
     * 更新粒子状态
     * @param deltaTime 时间间隔
     */
    public void update(float deltaTime) {
        if (!active) return;
        
        // 更新物理运动
        updatePhysics(deltaTime);
        
        // 更新视觉属性
        updateVisuals(deltaTime);
        
        // 更新生命周期
        currentLife -= deltaTime;
        
        // 检查是否死亡
        if (currentLife <= 0) {
            active = false;
        }
    }

    /**
     * 更新物理运动
     */
    private void updatePhysics(float deltaTime) {
        // 应用加速度
        velocityX += accelerationX * deltaTime;
        velocityY += accelerationY * deltaTime;
        
        // 应用重力
        velocityY += gravity * deltaTime;
        
        // 应用摩擦力
        velocityX *= friction;
        velocityY *= friction;
        
        // 更新位置
        x += velocityX * deltaTime;
        y += velocityY * deltaTime;
        
        // 更新旋转
        rotation += rotationSpeed * deltaTime;
    }

    /**
     * 更新视觉属性
     */
    private void updateVisuals(float deltaTime) {
        // 更新尺寸
        size += sizeVelocity * deltaTime;
        if (size < 0) size = 0;
        
        // 更新透明度
        if (alphaDecay > 0) {
            alpha -= alphaDecay * deltaTime;
        } else {
            // 基于生命周期的渐隐
            alpha = currentLife / maxLife;
        }
        
        if (alpha < 0) alpha = 0;
    }
    
    /**
     * 渲染粒子
     * @param painter 绘制器
     */
    public void render(Painter painter) {
        if (!active || alpha <= 0) return;
        
        // 设置透明度颜色
        int alphaColor = Color.argb(
            (int)(alpha * 255),
            Color.red(color),
            Color.green(color), 
            Color.blue(color)
        );
        
        painter.setColor(alphaColor);
        
        // 根据类型渲染
        switch (type) {
            case TYPE_CIRCLE:
                renderCircle(painter);
                break;
            case TYPE_RECT:
                renderRect(painter);
                break;
            case TYPE_POINT:
                renderPoint(painter);
                break;
            case TYPE_TEXT:
                renderText(painter);
                break;
        }
    }

    /**
     * 渲染圆形粒子
     */
    private void renderCircle(Painter painter) {
        int radius = (int)size;
        painter.fillOval((int)(x - radius), (int)(y - radius), radius * 2, radius * 2);
    }

    /**
     * 渲染矩形粒子
     */
    private void renderRect(Painter painter) {
        int halfSize = (int)(size / 2);
        painter.fillRect((int)(x - halfSize), (int)(y - halfSize), (int)size, (int)size);
    }

    /**
     * 渲染点状粒子
     */
    private void renderPoint(Painter painter) {
        painter.fillRect((int)x, (int)y, 1, 1);
    }

    /**
     * 渲染文字粒子
     */
    private void renderText(Painter painter) {
        if (text != null && !text.isEmpty()) {
            painter.drawString(text, (int)x, (int)y);
        }
    }
    
    /**
     * 重置粒子状态（用于对象池复用）
     */
    public void reset() {
        x = y = 0;
        velocityX = velocityY = 0;
        accelerationX = accelerationY = 0;
        maxLife = currentLife = 0;
        color = Color.WHITE;
        size = 2.0f;
        alpha = 1.0f;
        gravity = 0.0f;
        friction = 1.0f;
        active = false;
        type = TYPE_CIRCLE;
        text = "";
        rotation = 0;
        rotationSpeed = 0;
        sizeVelocity = 0;
        alphaDecay = 0.02f;
        bounce = 0;
    }
    
    /**
     * 设置重力
     * @param gravity 重力值
     */
    public void setGravity(float gravity) {
        this.gravity = gravity;
    }
    
    /**
     * 设置摩擦力
     * @param friction 摩擦力系数 (0.0-1.0)
     */
    public void setFriction(float friction) {
        this.friction = Math.max(0.0f, Math.min(1.0f, friction));
    }
    
    /**
     * 检查粒子是否激活
     * @return 是否激活
     */
    public boolean isActive() {
        return active;
    }
    
    /**
     * 强制停用粒子
     */
    public void kill() {
        active = false;
    }
    
    // ===== Getter 和 Setter 方法 =====
    
    public float getX() { return x; }
    public void setX(float x) { this.x = x; }
    
    public float getY() { return y; }
    public void setY(float y) { this.y = y; }
    
    public void setPosition(float x, float y) {
        this.x = x;
        this.y = y;
    }
    
    public float getVelocityX() { return velocityX; }
    public void setVelocityX(float velocityX) { this.velocityX = velocityX; }
    
    public float getVelocityY() { return velocityY; }
    public void setVelocityY(float velocityY) { this.velocityY = velocityY; }
    
    public void setVelocity(float vx, float vy) {
        this.velocityX = vx;
        this.velocityY = vy;
    }
    
    public float getAccelerationX() { return accelerationX; }
    public void setAccelerationX(float accelerationX) { this.accelerationX = accelerationX; }
    
    public float getAccelerationY() { return accelerationY; }
    public void setAccelerationY(float accelerationY) { this.accelerationY = accelerationY; }
    
    public void setAcceleration(float ax, float ay) {
        this.accelerationX = ax;
        this.accelerationY = ay;
    }
    
    public float getSize() { return size; }
    public void setSize(float size) { this.size = size; }
    
    public float getSizeVelocity() { return sizeVelocity; }
    public void setSizeVelocity(float sizeVelocity) { this.sizeVelocity = sizeVelocity; }
    
    public int getColor() { return color; }
    public void setColor(int color) { this.color = color; }
    
    public float getAlpha() { return alpha; }
    public void setAlpha(float alpha) { this.alpha = alpha; }
    
    public float getAlphaDecay() { return alphaDecay; }
    public void setAlphaDecay(float alphaDecay) { this.alphaDecay = alphaDecay; }
    
    public float getRotation() { return rotation; }
    public void setRotation(float rotation) { this.rotation = rotation; }
    
    public float getRotationSpeed() { return rotationSpeed; }
    public void setRotationSpeed(float rotationSpeed) { this.rotationSpeed = rotationSpeed; }
    
    public float getLifeTime() { return maxLife; }
    public void setLifeTime(float lifeTime) { this.maxLife = lifeTime; }
    
    public float getCurrentLife() { return currentLife; }
    
    public int getType() { return type; }
    public void setType(int type) { this.type = type; }
    
    public String getText() { return text; }
    public void setText(String text) { this.text = text; }
    
    public float getGravity() { return gravity; }
    public float getFriction() { return friction; }
    public float getBounce() { return bounce; }
    public void setBounce(float bounce) { this.bounce = bounce; }
    
    /**
     * 计算生命周期比例 (0-1)
     */
    public float getLifeRatio() {
        return currentLife / maxLife;
    }
    
    /**
     * 计算剩余生命比例 (1-0)
     */
    public float getRemainingLifeRatio() {
        return 1.0f - getLifeRatio();
    }
}